<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="校区名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="校区地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="校区地址"></el-input>
    </el-form-item>

    <el-form-item label="地址经度" prop="longitude">
      <el-input v-model="dataForm.longitude" placeholder="地址经度"></el-input>
    </el-form-item>
    <el-form-item label="校区纬度" prop="latitude">
      <el-input v-model="dataForm.latitude" placeholder="地址纬度"></el-input>
    </el-form-item>
    <el-form-item label="校区备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="校区备注"></el-input>
    </el-form-item>
    
    <el-form-item label="联系方式" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="联系方式"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { bd09ToGcj02 } from '@/utils'
import { isMobile } from '@/utils/validate'
  export default {
    data () {
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        dataForm: {
          id: 0,
          address: '',
          longitude: '',
          latitude: '',
          name: '',
          mobile: '',
          sort: '',
          isDeleted: '',
          gmtCreate: '',
          gmtModified: '',
          remark: ''
        },
        dataRule: {
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          mobile:[
             { required: true, message: '手机号不能为空', trigger: 'blur' },
             { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/eduschool/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.address = data.eduSchool.address
                this.dataForm.name = data.eduSchool.name
                this.dataForm.mobile = data.eduSchool.mobile
                this.dataForm.longitude = data.eduSchool.longitude
                this.dataForm.latitude = data.eduSchool.latitude
                this.dataForm.sort = data.eduSchool.sort
                this.dataForm.isDeleted = data.eduSchool.isDeleted
                this.dataForm.gmtCreate = data.eduSchool.gmtCreate
                this.dataForm.gmtModified = data.eduSchool.gmtModified
                this.dataForm.remark = data.eduSchool.remark
                this.gmtCreate = data.eduSchool.gmtCreate;
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/eduschool/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'address': this.dataForm.address,
                'name': this.dataForm.name,
                'mobile' : this.dataForm.mobile,
                'longitude' :  this.dataForm.longitude,
                'latitude' : this.dataForm.latitude,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
